include('/scripts/libs/jquery.imagesloaded.min.js');
include('http://tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=1380578740');
/************************************
 * afflatus.js
 * 提供灵感信息展示支持的体验文件
 *
 * @author Tianzhen(uimeet.com)
 * usage:
 *      $('._afflatus').afflatus();
 * note:
 *      只可用于<A>标签
 * description: 
 *      统一灵感弹出层的方式
 * */
(function ($) {

    
    $.fn.afflatus = function(conf) {
        this.fancybox({
            'margin': 0,
            'padding': 0,
            'showCloseButton': false,
            'onComplete': function () {
            }
        });
    };

})(jQuery);

/***********************************
 * @author Tianzhen(uimeet.com)
 *
 * 灵感信息相邻的两条信息快捷链接
 * 本方法获取目标标签的prev和next属性，
 * 来确定上一条下一条分别链接到何处
 *
 * usage:
 *      $('img._adjacent').aff_adjacent({...});
 * */
(function ($) {

    function Adjacent(rawTrigger, conf) {
        var self = this, 
            // 触发对象
            trigger = $(rawTrigger),
            // 图片的宽度
            w = trigger.width(),
            // 图片的高度
            h = trigger.height(),
            // 父容器
            parent = trigger.parent(),
            // 父容器宽度
            pw = parent.width(),
            // 父容器高度
            ph = parent.height(),
            // 上一条记录id
            prev = $.intval(trigger.attr('prev')),
            // 下一条记录id
            next = $.intval(trigger.attr('next')); 
       
        $.extend(self, {
            // 装载方法
            load: function () {
                if (prev > 0)
                    this.render_point(prev, 0, 'prev');
                if (next > 0)
                    this.render_point(next, w / 2, 'next');
                if ($.browser.msie) 
                    this.comp_ie(); // 为原始浏览器量身打造
            },
            // 渲染转向链接
            render_point: function (href, left, cur) {
                var p = $('<a id="adj-'+cur+'" href="/afflatus/' + href + '" class="' +cur+ '">&nbsp;</a>');
                p.css({
                    'width': w/2,
                    'height': h,
                    'position': 'absolute',
                    'left': (pw / 2 - w / 2) + left,
                    'top': 0
                });
                trigger.after(p);
            },
            comp_ie: function () {
                trigger.bind('mouseover mousemove mousedown mouseup click'
                        , function (evt) {
                            var h = trigger.offset().left,
                                g = w / 2,
                                next = $('#adj-next'),
                                prev = $('#adj-prev'),
                                i = !! next.length,
                                k = !! prev.length,
                                f = evt.pageX;
                            if (evt.type == 'click' 
                                && evt.target == trigger.get(0)) {
                                evt.preventDefault();
                                evt.stopPropagation();

                                var href = null;
                                if (i && h + g <= f)
                                    href = next.attr('href');
                                else if (k && h + g > f)
                                    href = prev.attr('href');
                                if (href)
                                    window.location.href = href;
                            }
                            // IE CSS中的cursor不支持相对路径
                            // 所以这里不用添加class的方式来改变鼠标
                            // 使用js指定
                            // 真是恼火，MD！！
                            if (i && h + g <= f) {
                                trigger.css('cursor', 'url('+$STATIC_PREFIX+'/default/images/next.cur), auto');
                                //trigger.addClass('next');
                                //trigger.removeClass('prev');
                            } else if (k && h + g > f) {
                                trigger.css('cursor', 'url('+$STATIC_PREFIX+'/default/images/prev.cur), auto');
                                //trigger.addClass('prev');
                                //trigger.removeClass('next');
                            } else {
                                trigger.css('cursor', null);
                                //trigger.removeClass('prev');
                                //trigger.removeClass('next');
                            }
                        }
                    );
            }
        });

        self.load();

    };
    
    $.fn.aff_adjacent = function (conf) {

        this.imagesLoaded(function () {
            this.each(function () {
                new Adjacent(this, conf);
            });
        }); 

    };

})(jQuery);

/**************************************
 * @author Tianzhen(uimeet.com)
 *
 * 灵感关注按钮事件
 * usage:
 *      $().aff_attention();
 * */
(function ($) {
    
    
    var att = '<a class="W_btn_b" href="javascript:;"><span><img src="'+$STATIC_PREFIX+'/images/common/transparent.gif" class="icon_add addbtn_b" /><font class="_text"></font></span></a>';
    var atted = '<div class="W_addbtn_even"><img src="'+$STATIC_PREFIX+'/images/common/transparent.gif" class="icon_add addbtn_d" /><span class="_text"></span>&nbsp;<span class="W_vline">|</span>&nbsp;<a class="W_linkb" href="javascript:;"><em>取消</em></a></div>';
    function Action(triggers, conf) {
        var self = this;
        var options = {
            'callback': null,
            'action': {
                    'text': '关注',
                    'type': 'follow',
                    'trigger': 'attention'
                },
            'actioned': {
                    'text': '已关注',
                    'type': 'unfollow'
                }
        };

        $.extend(options, conf);
        $.extend(self, {
            // 初装方法
            load: function() {
                this.apply(function (index, val) {
                    var the = $(this);
                    var actioned = the.attr('actioned');
                    var followlink = null, el = null, text, action;
                    if (actioned == 'yes') {
                        el = $(atted);
                        followlink = el.find('a');
                        type = options.actioned.type;
                        text = options.actioned.text;
                    } else {
                        el = followlink = $(att);
                        type = options.action.type;
                        text = options.action.text;
                    }
                    el.find('._text').html(text);
                    followlink.attr('action-type', type)
                        .attr('title', text)
                        .bind('click'
                            , {'based': val, 'self': followlink}
                            , self.action)
                        .login();
                    // 替换之前的
                    self.replaceData(the, el);
                    // 相应hash命令
                    self.hashCommand(actioned, followlink);
                });
            },
            apply: function(callable) {
                triggers.each(callable);
            },
            action: function(evt) {
                var the = $(evt.data.self);
                var based = $(evt.data.based);
                // 获取动作类型
                var at = the.attr('action-type');
                // 目前的状态
                var actioned = based.attr('actioned');
                if (the.data('loading')) {
                    // 如果已经在处理中
                    // 那么就不在重复处理
                    return false;
                }
                // 处于未进行动作状态
                if (actioned == 'no') {
                    // 操作进行中
                    self.loading(the);
                }
                // 执行动作
                execute('aff_' + at
                        , {'id':based.attr('action-data')}
                        , function (text) {
                            if (text == 'OK') {
                                based.attr('actioned', actioned == 'yes' ? 'no' : 'yes');
                            }
                            self.load();
                            // 处理成功后的回调
                            if ($.isFunction(options.callback)) {
                                // 执行回调
                                options.callback(at);
                            }
                        });
            },
            loading: function(elem) {
                elem.data('loading', true);
                elem.find('img').replaceWith('<b class="loading"></b>');
            },
            replaceData: function(elem, rep) {
                if (elem.data('el')) {
                    elem.data('el').replaceWith(rep);
                } else {
                    elem.append(rep);
                }
                elem.data('el', rep);
            },
            hashCommand: function(actioned, link) {
                if (actioned == 'no' 
                        && options.action.trigger 
                        && top.location.hash == '#' + options.action.trigger) {
                    // 处于未操作状态
                    // 且触发动作一致
                    link.trigger('click');
                    // 确保只触发一次
                    options.action.trigger = null;
                }
            }
        });

        self.load();

    };
    
    $.fn.aff_action = function (conf) {
        
        conf = conf || {};
        new Action(this, conf);

    };

})(jQuery);

/**************************************************
 *  @author Tianzhen(uimeet.com)
 *
 *  灵感的“去过”功能
 *
 */
(function ($) {

    var layer = $('<a class="hide"></a>');
    function BeenTo(rawTrigger, conf) {
        var self = this,
            trigger = $(rawTrigger),
            id = parseInt(trigger.attr('ref'));
        
        if (isNaN(id) && id <= 0) {
            // 无效的内码
            return;
        }

        trigger.click(function () {
            self.load();
        });

        $.extend(self, {
            load: function () {
                layer.load(id);
            },
            hashCommand: function() {
                if (location.hash == '#beento') {
                    self.load();
                }
            }
        });

        self.hashCommand();
        
    };

    function init() {
        $(document).append(layer);
        layer.fancybox({
            'type': 'iframe',
            'margin': 0,
            'padding': 0,
            'width':363,
            'height': 288,
            'showCloseButton': false
        });
        layer.extend({
            load: function (id) {
                this.attr('href', '/afflatus/beento/' + id);
                this.trigger('click');
            }
        });
    };
    
    $.fn.beento = function (conf) {

        init();
    
        this.each(function () {
        
            new BeenTo(this, conf);

        });

    };

})(jQuery)
